<script lang="ts">
export default {
  name: "PieOther"
};
</script>

<script setup lang="ts">
import { ECharts } from "echarts";
import echarts from "/@/plugins/echarts";
import { onBeforeMount, onMounted, nextTick, ref } from "vue";
import { tryOnUnmounted, useTimeoutFn } from "@vueuse/core";

import {
  addResizeListener,
  removeResizeListener
} from "element-plus/lib/utils/resize-event";

const pieOther = ref<HTMLElement | null>(null) as any;

let echartInstance: ECharts;

function initechartInstance() {
  const echartDom = document.querySelector(".pieOther");
  if (!echartDom) return;
  // @ts-ignore
  echartInstance = echarts.init(echartDom);
  echartInstance.clear(); //清除旧画布 重新渲染

  echartInstance.setOption({
    color: ["#00e6ad", "#59a0ff", "#ffc500"],
    tooltip: {
      trigger: "item"
    },
    // legend: {
    //   orient: "vertical",
    //   right: true
    // },
    // legend: {
    //   top: "5%",
    //   left: "center"
    // },
    series: [
      {
        name: "信息",
        type: "pie",
        radius: "60%",
        data: [
          { value: 1079, name: "watchers" },
          { value: 1079, name: "star" },
          { value: 204, name: "forks" },
          { value: 3, name: "open_issues" }
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: "rgba(0, 0, 0, 0.5)"
          }
        }
      }
    ]
  });
}

onBeforeMount(() => {
  nextTick(() => {
    initechartInstance();
  });
});

onMounted(() => {
  nextTick(() => {
    addResizeListener(pieOther.value, resetRender);
  });
  // useEventListener("resize", () => {
  //   console.log("resize");
  //   if (!echartInstance) return;
  //   useTimeoutFn(() => {
  //     echartInstance.resize();
  //   }, 180);
  // });
});

const resetRender = () => {
  if (!echartInstance) return;
  useTimeoutFn(() => {
    echartInstance.resize();
  }, 180);
};

tryOnUnmounted(() => {
  if (!echartInstance) return;
  echartInstance.dispose();
  echartInstance = null;
  removeResizeListener(pieOther.value, resetRender);
});
</script>

<template>
  <div ref="pieOther">
    <div class="title">
      <slot> </slot>
    </div>
    <div class="pieOther"></div>
  </div>
</template>

<style lang="scss" scoped>
.title {
  font-size: 16px;
  color: #409eff;
  margin: 10px 0;
  height: 24px;
}
.pieOther {
  width: 100%;
  height: 250px;
}
</style>
